package com.badshun.ojs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class OJSRuntime {
    private ScriptLoader loader;
    private OJSLexer lexer;

    private HashMap<String, HashMap<String, OJSObject>> memory;

    private boolean isVarMemoryEnable = false;
    private boolean isFuncMemoryEnable = false;
    private boolean isClassMemoryEnable = false;

    public OJSRuntime() {
        memory = new HashMap<String, HashMap<String, OJSObject>>();
    }

    public void applyForMemory(OJSObject obj) {
        if (obj.getType() == OJSObject.TYPE.VAR) {
            if (!isVarMemoryEnable) {
                HashMap<String, OJSObject> varMemory = new HashMap<String, OJSObject>();
                memory.put("VAR", varMemory);

                isVarMemoryEnable = true;
            }

            HashMap<String, OJSObject> varMemory = memory.get("VAR");
            varMemory.put(obj.name, obj);
        }

        if (obj.getType() == OJSObject.TYPE.FUNC) {
            if (!isFuncMemoryEnable) {
                HashMap<String, OJSObject> funcMemory = new HashMap<String, OJSObject>();
                memory.put("FUNC", funcMemory);

                isFuncMemoryEnable = true;
            }

            HashMap<String, OJSObject> funcMemory = memory.get("FUNC");
            funcMemory.put(obj.name, obj);
        }

    }

    public String getInMemory(OJSObject.TYPE type, String name) {
        HashMap<String, OJSObject> _memory = null;

        if (type == OJSObject.TYPE.VAR) {
            _memory = memory.get("VAR");
        }

        if (_memory == null) {
            return null;
        } else {
            if (_memory.containsKey(name)) {
                return _memory.get(name).content;
            }

            return null;
        }
    }

    public void exec(String path) {
        loader = new ScriptLoader(path);
        loader.printContent();

        lexer = new OJSLexer(loader.getScriptContenList(), this);
    }
}
